package com.avic.rong.userOrder.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.avic.rong.R;
import com.avic.rong.userOrder.activity.OrderPathActivity;
import com.avic.rong.userOrder.bean.OrderDetailsBean;

import java.util.List;

/**
 * Created by XinEr on 2019/7/18.
 * Describe:
 */
public class OrderGoodsDetailsListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private int type = 1;
    private List<OrderDetailsBean.DeliveryListBean> deliveryList;
    private OrderDetailsBean orderDetailsBean;


    public OrderGoodsDetailsListAdapter(Context context, List<OrderDetailsBean.DeliveryListBean> deliveryList) {
        this.context = context;
        this.deliveryList = deliveryList;

    }

    public OrderDetailsBean getOrderDetailsBean() {
        return orderDetailsBean;
    }

    public void setOrderDetailsBean(OrderDetailsBean orderDetailsBean) {
        this.orderDetailsBean = orderDetailsBean;
    }

    @Override
    public GoodsPachageViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if (i == type) {
            View inflate = LayoutInflater.from(context).inflate(R.layout.order_goods_package_item, viewGroup, false);
            return new GoodsPachageViewHolder(inflate);
        } else {
            View inflate = LayoutInflater.from(context).inflate(R.layout.order_goods_package_item, viewGroup, false);
            return new GoodsPachageViewHolder(inflate);
        }


    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        if (viewHolder instanceof GoodsPachageViewHolder) {
            ((GoodsPachageViewHolder) viewHolder).tv_goods_package_number.setText("包裹" + (i + 1));
            ((GoodsPachageViewHolder) viewHolder).tv_kuaidi_way.setText(deliveryList.get(i).getShippingName());
            ((GoodsPachageViewHolder) viewHolder).tv_kuaidi_tel.setText(deliveryList.get(i).getInvoiceNo()+"");

            ((GoodsPachageViewHolder) viewHolder).ll_order_path.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context, OrderPathActivity.class);
                    intent.putExtra("order_id",deliveryList.get(i).getOrderId()+"");
                    intent.putExtra("sequence",(i + 1));
                    context.startActivity(intent);
                }
            });
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context,
                    LinearLayoutManager.VERTICAL, false) {
                @Override
                public boolean canScrollVertically() {
                    return false;
                }
            };
            OrderDetailsBean.DeliveryListBean deliveryListBean = deliveryList.get(i);
            List<OrderDetailsBean.DeliveryListBean.ItemsBean> items = deliveryListBean.getItems();
            if (items!=null&&items.size()>0){
                ((GoodsPachageViewHolder) viewHolder).ll_fahuo_list.setVisibility(View.VISIBLE);
            }else {
                ((GoodsPachageViewHolder) viewHolder).ll_fahuo_list.setVisibility(View.GONE);
            }
            ((GoodsPachageViewHolder) viewHolder).recy_list.setLayoutManager(linearLayoutManager);
            OrderGoodsDetailsItemAdapter orderGoodsDetailsItemAdapter = new OrderGoodsDetailsItemAdapter(context, items,orderDetailsBean);
            ((GoodsPachageViewHolder) viewHolder).recy_list.setAdapter(orderGoodsDetailsItemAdapter);

        }
    }


    @Override
    public int getItemViewType(int position) {
        return type;
    }

    @Override
    public int getItemCount() {
        return deliveryList.size();
    }


    public class ViewHolder extends RecyclerView.ViewHolder {
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
        }
    }

    public class GoodsPachageViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_goods_package_number;
        private RecyclerView recy_list;
        private TextView tv_kuaidi_way;
        private TextView tv_kuaidi_tel;
        private ImageView tv_wuliu;
        private LinearLayout ll_order_path;
        private LinearLayout ll_fahuo_list;
        public GoodsPachageViewHolder(View itemView) {
            super(itemView);
            tv_goods_package_number = itemView.findViewById(R.id.tv_goods_package_number);
            recy_list = itemView.findViewById(R.id.recy_list);
            tv_kuaidi_way = itemView.findViewById(R.id.tv_kuaidi_way);
            tv_kuaidi_tel = itemView.findViewById(R.id.tv_kuaidi_tel);
            tv_wuliu = itemView.findViewById(R.id.tv_wuliu);
            ll_order_path = itemView.findViewById(R.id.ll_order_path);
            ll_fahuo_list = itemView.findViewById(R.id.ll_fahuo_list);
        }
    }
}
